package cn.sitia.wallet.service.impl;

import cn.sitia.wallet.domain.UserWallet;
import cn.sitia.wallet.domain.WalletTransaction;
import cn.sitia.wallet.enums.WalletTransactionType;
import cn.sitia.wallet.mapper.UserWalletMapper;
import cn.sitia.wallet.mapper.WalletTransactionMapper;
import cn.sitia.wallet.service.IUserWalletService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class UserWalletServiceImpl extends ServiceImpl<UserWalletMapper, UserWallet> implements IUserWalletService {
    @Autowired
    private WalletTransactionMapper walletTransactionMapper;
    /**
     * 查询用户钱包余额
     */
    public BigDecimal getUserWalletBalance(int userId) {
        QueryWrapper<UserWallet> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        UserWallet userWallet = super.getOne(wrapper);
        return userWallet != null ? userWallet.getBalance() : BigDecimal.ZERO;
    }

    /**
     * 用户消费接口
     */
    @Transactional
    public void purchase(BigDecimal amount) {
        Integer userId = SecurityUtil.getLoginUserId();//伪代码，获取当前用户登录的id
        //Integer userId = 1;
        Assert.notNull(userId,"非法请求");
        UserWallet userWallet = super.getOne(new QueryWrapper<UserWallet>().eq("user_id", userId));
        if (userWallet == null ) {
            throw new RuntimeException("交易失败");
        }
        if (userWallet.getBalance().compareTo(amount) < 0 ) {
            throw new RuntimeException("余额不足");
        }

        // 更新余额
        userWallet.setBalance(userWallet.getBalance().subtract(amount));
        super.updateById(userWallet);

        // 插入交易记录
        Date time = new Date();
        WalletTransaction walletTransaction = new WalletTransaction();
        walletTransaction.setUserId(userId);
        walletTransaction.setAmount(amount);
        walletTransaction.setDescription(time + "：消费了" + amount);
        walletTransaction.setTransactionType(WalletTransactionType.PURCHASE);
        walletTransaction.setCreatedAt(new Date());
        walletTransactionMapper.insert(walletTransaction);
    }

    /**
     * 用户退款接口
     */
    @Transactional
    public void refund(BigDecimal amount) {
        Integer userId = SecurityUtil.getLoginUserId();//伪代码，获取当前用户登录的id
        //Integer userId = 1;
        Assert.notNull(userId,"非法请求");
        // 查询钱包余额
        UserWallet userWallet = super.getOne(new QueryWrapper<UserWallet>().eq("user_id", userId));
        if (userWallet == null) {
            throw new RuntimeException("系统繁忙");
        }

        // 更新余额
        userWallet.setBalance(userWallet.getBalance().add(amount));
        super.updateById(userWallet);

        // 插入交易记录
        Date time = new Date();
        WalletTransaction walletTransaction = new WalletTransaction();
        walletTransaction.setUserId(userId);
        walletTransaction.setAmount(amount);
        walletTransaction.setDescription(time + "：退款：" + amount);
        walletTransaction.setTransactionType(WalletTransactionType.REFUND);
        walletTransaction.setCreatedAt(time);
        walletTransactionMapper.insert(walletTransaction);
    }


}
